/* 
Scenic Graphics Library
Copyright (C) 2007 Jouni Tulkki

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USApackage scenic;
*/

package scenic.toolkit;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.util.Vector;

class RootMouseHandler implements MouseListener, MouseMotionListener,
MouseWheelListener {
	private RootPane root;
	private Vector<ComponentView> hierarchy = new Vector<ComponentView>();
	
	RootMouseHandler(RootPane root) {
		this.root = root;
	}
	
	private void updateHierarchy(Vector<ComponentView> nh, MouseEvent e) {
		int i;
		Point2D.Double p = new Point2D.Double(e.getX(), e.getY());
		Point2D.Double p2 = new Point2D.Double();
		
		for(i = 0; i < Math.min(hierarchy.size(), nh.size()); i++)
			if(hierarchy.elementAt(i) != nh.elementAt(i))
				break;
		
		for(int j = hierarchy.size() - 1; j >= i; j--)
		{
			ComponentView c = hierarchy.elementAt(j);
			Vector<scenic.toolkit.event.MouseListener> listeners = c.getMouseListeners();
			
			try {
				c.getTotalTransform().createInverse().transform(p, p2);
			} catch (NoninvertibleTransformException e1) {
			}
			
			scenic.toolkit.event.MouseEvent te = 
				new scenic.toolkit.event.MouseEvent(c, p2, 0, e.getButton());
			
			for(scenic.toolkit.event.MouseListener l : listeners)
				l.mouseExited(te);
		}
		
		for(int j = i; j < nh.size(); j++)
		{
			ComponentView c = nh.elementAt(j);
			Vector<scenic.toolkit.event.MouseListener> listeners = c.getMouseListeners();
			
			try {
				c.getTotalTransform().createInverse().transform(p, p2);
			} catch (NoninvertibleTransformException e1) {
			}
			
			scenic.toolkit.event.MouseEvent te = 
				new scenic.toolkit.event.MouseEvent(c, p2, 0, e.getButton());
			
			for(scenic.toolkit.event.MouseListener l : listeners)
				l.mouseEntered(te);
		}
		
		hierarchy = nh;
	}
	
	private Vector<ComponentView> getHierarchy(Point2D p) {
		Vector<ComponentView> h = new Vector<ComponentView>();		
		ComponentView c = root.getContent();
		ComponentView child;
		
		h.add(c);
		try {
			while((child = c.getChildAt(p)) != null) {
				c = child;
				p = c.getTransform().createInverse().transform(p, null);
				h.add(c);
			}
		} catch (NoninvertibleTransformException e) {
		}
		
		return h;
	}
	
	public void mouseDragged(MouseEvent e) {
	}

	public void mouseMoved(MouseEvent e) {
		Point2D p = new Point2D.Double(e.getX(), e.getY());
		
		Vector<ComponentView> h = getHierarchy(p);
		
		updateHierarchy(h, e);
	}

	public void mouseClicked(MouseEvent e) {
		Point2D p = new Point2D.Double(e.getX(), e.getY());
		ComponentView c = root.getContent();
		ComponentView child;
		
		try {
			while((child = c.getChildAt(p)) != null) {
				c = child;
				p = c.getTransform().createInverse().transform(p, null);
			}
		} catch (NoninvertibleTransformException e1) {
		}
		
		scenic.toolkit.event.MouseEvent te;
		
		te = new scenic.toolkit.event.MouseEvent(c, p, 0, e.getButton());
		
		Vector<scenic.toolkit.event.MouseListener> listeners = c.getMouseListeners();
		
		for(scenic.toolkit.event.MouseListener l : listeners)
			l.mouseClicked(te);
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
	}

	public void mouseReleased(MouseEvent e) {
	}

	public void mouseWheelMoved(MouseWheelEvent e) {
	}
}
